﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace ConsoleApplication2
{
    public class GroupModel
    {
        public int GroupId { get; set; }
        public string Msg { get; set; }
    }

    class Program
    {
        public static List<GroupModel> data = new List<GroupModel>();
        static void Main(string[] args)
        {
            List<int> dd = new List<int>();

            for (int i = 0; i < 1000; i++)
            {

                Random rd = new Random(Guid.NewGuid().GetHashCode());
                var result = rd.Next(10);
                dd.Add(result);

                
            }
            var gd = dd.Where(s => s < 1);

            return;
            //Test();return;

            List<int> list = Init();

            var nList = ListToPage(list, 20000);

            Stopwatch stw = new Stopwatch();

            stw.Start();

            var tasks = new Task[nList.Count];

            for (int index = 0; index < nList.Count; index++)
            {
                var n = nList[index];
                tasks[index] = System.Threading.Tasks.Task.Factory.StartNew(ary =>
                {
                    try
                    {
                        var clist = (ary as object[])[0] as List<int>;
                        var i = (ary as object[])[1];
                        SaveSpotGroup(clist, i);
                    }
                    catch (Exception ex)
                    {
                    }
                }, new object[] { n, index });
            }
            
            //等待多线程全部执行完毕
            Task.WaitAll(tasks);
            stw.Stop();

            Console.WriteLine("共用时：" + (stw.ElapsedMilliseconds) + "毫秒");

            foreach (var groupModel in data)
            {
                Console.WriteLine("分组【" + groupModel.GroupId + "】输出完成" + groupModel.Msg);
            }

            Console.ReadLine();
        }

        public static List<int> Init()
        {
            List<int> list = new List<int>();

            for (int i = 0; i < 100000; i++)
            {
                list.Add(i);
            }

            return list;
        }

        /// <summary>
        /// 处理
        /// </summary>
        public static void SaveSpotGroup(List<int> list, object index)
        {
            Stopwatch stw = new Stopwatch();

            stw.Start();
            try
            {
                foreach (var str in list)
                {
                    if (str%9 == 0 && str%8 == 0 && str%7 == 0 && str%6 == 0 && str%5 == 0 && str%4 == 0)
                    {
                        Console.WriteLine("分组【" + index + "】输出：" + str); 
                    }
                }
            }
            catch (Exception ex)
            {

            }

            stw.Stop();

            string msg = "，共用时：" + (stw.ElapsedMilliseconds) + "毫秒";
            GroupModel model = new GroupModel()
            {
                GroupId = (int)index,
                Msg = msg
            };

            if (!data.Contains(model))
            {
                data.Add(model);
            }

            //Console.WriteLine("");
        }

        public static List<List<int>> ListToPage(List<int> list, int pageSize)
        {
            var result = new List<List<int>>();

            var sectionCount = (list.Count / pageSize) + (list.Count % pageSize > 0 ? 1 : 0);

            for (int i = 1; i <= sectionCount; i++)
            {
                var beginRn = (i - 1) * pageSize;
                var endRn = i * pageSize;

                var sectionList = list.Where(r => r > beginRn && r <= endRn).ToList();

                result.Add(sectionList);
            }

            return result;
        }
    }
}
